package com.ajmd.onlineexam.service;

import com.ajmd.onlineexam.entity.MessagePO;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.mapper.MessageMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author ajmd
 * @since 2023/5/3 23:27
 */
@Service
public class MessageService {
    @Resource
    private MessageMapper messageMapper;

    public int addMessage(MessagePO messagePO){
        messagePO.setId(UUID.randomUUID().toString());
        return messageMapper.insert(messagePO);
    }


    /**
     * 批量添加消息（单个考试的通知）
     * @param studentIds
     * @param sendAccount
     * @param msg
     * @return
     */
    @Async
    public int batchInsertByStudentIds(Set<String> studentIds, String sendAccount, String msg){
        List<MessagePO> messagePOS = new ArrayList<>();
        //往消息表中增加记录
        for(String account:studentIds){
            MessagePO messagePO = new MessagePO();
            messagePO.setId(UUID.randomUUID().toString());
            messagePO.setContent(msg);
            messagePO.setReceiver(account);
            messagePO.setReceiverType(StaticFinalValue.STUDENT);
            messagePO.setSender(sendAccount);
            messagePOS.add(messagePO);
        }
        return messageMapper.insertBatchSomeColumn(messagePOS);
    }

    public int batchInsert(Map<String, Map<String,Object>> stringMapMap){
        List<MessagePO> messagePOS = new ArrayList<>();
        stringMapMap.forEach((key,value)->{
            String msg = (String) value.get("msg");
            String sendAccount = (String) value.get("sendAccount");
            Set<String> studentIds = (Set<String>) value.get("studentIds");
            Integer sendType = (Integer)value.get("sendType");
            for(String account:studentIds){
                MessagePO messagePO = new MessagePO();
                messagePO.setId(UUID.randomUUID().toString());
                messagePO.setContent(msg);
                messagePO.setReceiver(account);
                messagePO.setReceiverType(StaticFinalValue.STUDENT);
                messagePO.setSender(sendAccount);
                messagePO.setSenderType(sendType);
                messagePO.setStatus(0);//未读
                messagePO.setSendTime(new Timestamp(System.currentTimeMillis()));
                messagePOS.add(messagePO);
            }
        });
        return messageMapper.insertBatchSomeColumn(messagePOS);
    }

    /**
     * 根据用户账号和用户类型查询消息
     * @param account
     * @param userType
     * @return
     */
    public List<MessagePO> selectList(String account,Integer userType){
        LambdaQueryWrapper<MessagePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MessagePO::getReceiver,account)
                .eq(MessagePO::getReceiverType,userType);
        return messageMapper.selectList(lambdaQueryWrapper);
    }

    //todo 创建定时任务定期清理消息表 ， 每隔3个月或者一定时间清理一次
}
